<template>
  <div class="table-box">
    <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="name" label="商品名称" align="center" />
          <el-table-column prop="description" label="商品描述" align="center" />
          <el-table-column prop="amount" label="钻石数" align="center" />
          <el-table-column prop="ios_price" label="ios价格" align="center" />
          <el-table-column prop="android_price" label="android价格" align="center" />
          <el-table-column prop="app_mark" label="APP版本" align="center" />
          <!-- goods_id: "谷歌市场商品id",
    price: "基准定价价格",
    show_price: "展示价格",
    currency: "基准币种",
    currency_symbol: "基准币种符号",
    show_currency: "展示法币",
    show_symbol: "展示法币符号",
          rank: "排序"-->
          <el-table-column prop="goods_id" label="谷歌市场商品id" align="center" />
          <el-table-column prop="price" label="基准定价价格" align="center" />
          <el-table-column prop="show_price" label="展示价格" align="center" />
          <el-table-column prop="currency" label="基准币种" align="center" />
          <el-table-column prop="currency_symbol" label="基准币种符号" align="center" />
          <el-table-column prop="show_currency" label="展示法币" align="center" />
          <el-table-column prop="show_symbol" label="展示法币符号" align="center" />
          <el-table-column prop="rank" label="排序" align="center" />

          <el-table-column label="操作" align="center" fixed="right" width="200">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
              >更新</el-button>
              <el-button type="danger" @click="handleOperate(scope.$index, scope.row, 'delete')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatterTime } from "@/utils/utils";
import {
  shopGoodsListApi,
  shopGoodsAddApi,
  shopGoodsUpdateApi,
  shopGoodsDelApi
} from "@/api/slotsgame/shopGoods/shopGoods";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const btnType = ref(null);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const dialogFormData = {
  id: "",
  name: "",
  description: "",
  amount: "",
  ios_price: "",
  android_price: "",
  app_mark: "",
  goods_id: "",
  price: "",
  show_price: "",
  currency: "",
  currency_symbol: "",
  show_currency: "",
  show_symbol: "",
  rank: ""
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await shopGoodsListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 查询
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await shopGoodsListApi(data);

  let head = {
    id: "ID",
    name: "商品名称",
    description: "商品描述",
    amount: "钻石数",
    ios_price: "ios价格",
    android_price: "android价格",
    app_mark: "APP版本",
    goods_id: "谷歌市场商品id",
    price: "基准定价价格",
    show_price: "展示价格",
    currency: "基准币种",
    currency_symbol: "基准币种符号",
    show_currency: "展示法币",
    show_symbol: "展示法币符号",
    rank: "排序"
  };
  exportToExcel(res.data.results, head, "Slotsgame-商品列表");
};
//关闭
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async param => {
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await shopGoodsAddApi(param);
      ElMessage({
        message: res.message,
        type: "success"
      });
    }
    if (btnType.value == "edit") {
      const res = await shopGoodsUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success"
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogVisible.value = true;
    Object.assign(dialogForm, { ...row });
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此商品?", "温馨提示", {
      type: "warning"
    }).then(async () => {
      const res = await shopGoodsDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
